package de.cdtm.whackathon.bloodwine.client;

import org.cobogw.gwt.waveapi.gadget.client.ModeChangeEvent;
import org.cobogw.gwt.waveapi.gadget.client.ModeChangeEventHandler;
import org.cobogw.gwt.waveapi.gadget.client.ParticipantUpdateEvent;
import org.cobogw.gwt.waveapi.gadget.client.ParticipantUpdateEventHandler;
import org.cobogw.gwt.waveapi.gadget.client.StateUpdateEvent;
import org.cobogw.gwt.waveapi.gadget.client.StateUpdateEventHandler;
import org.cobogw.gwt.waveapi.gadget.client.WaveGadget;

import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.GWT.UncaughtExceptionHandler;
import com.google.gwt.gadgets.client.Gadget;
import com.google.gwt.gadgets.client.UserPreferences;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.RootPanel;

/**
 * 
 */
@Gadget.ModulePrefs(title = "Bloodwine", title_url = "http://code.google.com/p/bloodwine/")
@Gadget.InjectModulePrefs(files = { "ModulePrefsRpc", "ModulePrefsDynamicHeight" })
public class Bloodwine extends WaveGadget<UserPreferences> {
	@Override
	protected void init(UserPreferences preferences) {
		final BloodwinePanel bloodwinePanel = new BloodwinePanel(this);

		GWT.setUncaughtExceptionHandler(new UncaughtExceptionHandler() {
			@Override
			public void onUncaughtException(Throwable throwable) {
				StringBuilder str = new StringBuilder();
				while(throwable != null) {
					str.append(throwable.getLocalizedMessage());
					str.append("\n");
					throwable = throwable.getCause();
				}
				Window.alert(str.toString());
			}
		});
		
		getWave().addParticipantUpdateEventHandler(
				new ParticipantUpdateEventHandler() {
					public void onUpdate(ParticipantUpdateEvent event) {
						bloodwinePanel.updateResult();
					}
				});
		
		getWave().addStateUpdateEventHandler(new StateUpdateEventHandler() {
			public void onUpdate(StateUpdateEvent event) {
				bloodwinePanel.updateResult();
			}
		});
		
		getWave().addModeUpdateEventHandler(new ModeChangeEventHandler() {
			public void onUpdate(ModeChangeEvent event) {
				//bloodwinePanel.updateResult();
			}
		});
		
		RootPanel.get().add(bloodwinePanel);
		RootPanel.get().setStyleName("rootPanel");

		bloodwinePanel.updateResult();
	}
}
